package com.hngy.xpq.android_practice.ui.dashboard;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.hngy.xpq.android_practice.R;
import com.hngy.xpq.android_practice.adapter.CommAdapter;
import com.hngy.xpq.android_practice.bean.Comm;
import com.hngy.xpq.android_practice.bean.Recording;
import com.hngy.xpq.android_practice.database.SQLiteComm;
import com.hngy.xpq.android_practice.database.SQLiteRecording;
import com.hngy.xpq.android_practice.ui.home.User_Comm;

import java.util.List;

public class DashboardFragment extends Fragment {
    ListView dashboard_list;
    GridView dashboard_comm_list;
    SQLiteComm sqLiteComm;
    SQLiteRecording sqLiteRecording;
    List<Comm> list;
    List<Recording> recordings;
    String types = null;
    CommAdapter adapter;
    SharedPreferences comms,main;
    Recording r;
    Comm c;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_dashboard, container, false);

        dashboard_list = root.findViewById(R.id.dashboard_list);
        dashboard_comm_list = root.findViewById(R.id.dashboard_comm_list);
        DashboardListItem();
        return root;
    }

    private void DashboardListItem() {
        dashboard_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String[] indexTypes = getResources().getStringArray(R.array.dashboard_list);
                types = indexTypes[position];
                DashboardData();
            }
        });
        dashboard_comm_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                main = getActivity().getSharedPreferences("main",Context.MODE_PRIVATE);
                comms = getActivity().getSharedPreferences("comm",Context.MODE_PRIVATE);
                Intent intent = new Intent(getActivity(), User_Comm.class);
                c = list.get(position);
                SharedPreferences.Editor editor = comms.edit();
                editor.putString("name",c.getMerchant_name());
                editor.putString("price",c.getComm_price());
                editor.putString("phone",c.getComm_phone());
                editor.putString("comm_name",c.getComm_name());
                editor.putString("position","0");
                editor.putString("math",c.getComm_name());
                editor.commit();
                //浏览查重
                if (recordings != null){
                    recordings.clear();
                }
                recordings = sqLiteRecording.recordingQuery(main.getString("name",""));
                int index = 0;
                for (int x = 0 ; x < recordings.size() ; x++ ){
                    if (recordings.get(x).getRecording_name().equals(c.getComm_name())){
                        index = 1;
                    }
                }
                //添加浏览记录
                if ( index == 0 ){
                    r = new Recording();
                    r.setMerchant_name(c.getMerchant_name());
                    r.setRecording_name(c.getComm_name());
                    r.setUser_name(main.getString("name",""));
                    r.setLikes("recording");
                    if (sqLiteRecording.recording_add(r)){
                        Log.i("保存成功","");
                    } else {
                        Log.i("保存失败","");
                    }
                }
                startActivity(intent);
            }
        });
    }

    private void DashboardData() {
        c = new Comm();
        sqLiteComm = new SQLiteComm(getActivity());
        sqLiteRecording = new SQLiteRecording(getActivity());
        if (list != null){
            list.clear();
        }
        list = sqLiteComm.CommTypesQuery(types);
        adapter = new CommAdapter(getActivity(),list);
        dashboard_comm_list.setAdapter(adapter);
    }

    private void IndexToast(String index) {
        Toast.makeText(getActivity(),index,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1&&resultCode==2){
            DashboardData();
        }
    }
}